package com.bnlr.lrparking.appui.carport.filtrate;

import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseSectionQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.bnlr.lrparking.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author : tian
 * time   : 2018/03/29
 * desc   : 筛选适配器
 */

public class FiltrateConditionAdapter extends BaseSectionQuickAdapter<FiltrateConditionBean,BaseViewHolder> {
    private Map<String,Integer> tagMap = new HashMap<>();

    private List<FiltrateConditionBean> dataS;
    public FiltrateConditionAdapter(List<FiltrateConditionBean> data,Map<String,Integer> tagMap) {
        super( R.layout.item_filtrae_body_layout, R.layout.item_filtrae_head_layout, data);
        this.dataS = data;
        this.tagMap = tagMap;
    }

    @Override
    protected void convertHead(BaseViewHolder helper, FiltrateConditionBean item) {
        helper.setText(R.id.tv_filtrate_head,item.header);
    }

    @Override
    protected void convert(final BaseViewHolder helper, final FiltrateConditionBean item) {
        TextView tvBody = helper.getView(R.id.tv_filtrate_body);
        tvBody.setText(item.t.getTxt());
        if (item.t.isSelected()) {
            tvBody.setBackgroundResource(R.mipmap.pay_moneybg_click);
            tvBody.setTextColor(mContext.getResources().getColor(R.color.write));
        } else {
            tvBody.setBackgroundResource(R.mipmap.pay_moneybg_nor);
            tvBody.setTextColor(mContext.getResources().getColor(R.color.text_color));
        }
        tvBody.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String key = item.t.getKey();
                if (tagMap.containsKey(key)) {
                    if (tagMap.get(key) == helper.getAdapterPosition()) {
                        //  点击同一个
                        item.t.setSelected(false);
                        tagMap.remove(key);
                    } else {
                        mData.get(tagMap.get(key)).t.setSelected(false);
                        item.t.setSelected(true);
                        tagMap.put(key,helper.getAdapterPosition());
                    }
                } else {
                    tagMap.put(key,helper.getAdapterPosition());
                    item.t.setSelected(true);
                }
                notifyDataSetChanged();
            }
        });
    }
    public Map getParams () {
        Map map = new HashMap();
        for (FiltrateConditionBean item: mData
             ) {
            if (!item.isHeader) {
                if (item.t.isSelected()) {
                    String mapKey = item.t.getKey();
                    if (map.containsKey(mapKey)) {
                        List<String> list = (List<String>) map.get(mapKey);
                        list.add(item.t.getValue());
                        map.put(mapKey,list);
                    } else {
                        List<String> list = new ArrayList<>();
                        list.add(item.t.getValue());
                        map.put(mapKey,list);
                    }
                }
            }
        }
        return map;
    }
    public Map<String,Integer> getMap() {
        return tagMap;
    }
}
